<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use think\Db;

/**
 * 物流类型模型
 */
class StockType extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'wh_stock_type';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取分组
     * @param int $id ID
     * @return array
     */
    public static function getTypeName($id = 0)
    {
        return self::where(['id'=>$id])->value('title');
    }
    /**
     * 获取分组
     * @param int $pid 父ID
     * @param bool $icon 图标，颜色
     * @param array $where 条件
     * @return array
     */
    public static function getGroup($pid = 0,$icon = false,$where = [])
    {
        $map['status'] = 1;
        $map['pid'] = $pid;
        if(!empty($where))$map = $where;
        if($icon)$menus = self::where($map)->order('sort,id')->column('title,icon,color','id');
        else $menus = self::where($map)->order('sort,id')->column('title','id');
        return $menus;
    }
    public static function getAjaxGroup($pid = 0)
    {
        $map['status'] = 1;
        $map['pid'] = $pid;
        $menus = self::where($map)->field('id as `key`,title as `value`')->select();
        return $menus;
    }

    /**
     * 根据分组获取子分组
     * @param int $pid 父ID
     * @param bool|string $fields 要返回的字段
     * @param array $map 查找条件
     * @return array
     */
    public static function getMenusByGroup($pid = 0, $fields = true, $map = [])
    {
        $map['pid'] = $pid;
        return self::where($map)->order('sort,id')->column($fields, 'id');
    }

    /**
     * 获取分组层数
     * @param int $pid 父ID
     * @param int $level 分组层数
     * @return array
     */
    public static function getLevle($pid = 0,$level = 0)
    {
        $level ++;
        $map['status'] = 1;
        $map['id'] = $pid;
        if(!empty($where))$map = $where;
        $pid = Db::name('wh_stock_type')->where($map)->value('pid');
        if(!empty($pid))return self::getLevle($pid,$level);
        return $level;
    }

    /**
     * 根据物料类型ID获取已互相关联的物料类型ID,包括父ID
     * @param array $data
     * @return array
     */
    public static function getLinkTypeIds($data = [])
    {
        $type_id = '';
        if(!empty($data['type_id3']))$type_id = $data['type_id3'];
        elseif(!empty($data['type_id2']))$type_id = $data['type_id2'];
        elseif(!empty($data['type_id']))$type_id = $data['type_id'];
        elseif(!empty($data['type_pid']))$type_id = $data['type_pid'];
        $map['status'] = 1;
        $map['link_type_id'] = $type_id;
        if(!empty($where))$map = $where;
        $link_type_id = Db::name('wh_stock_type')->where($map)->value('id');

        //获取，并返回，关联的物料类型所有父ID
        return self::getTypePids($link_type_id);
    }

    /**
     * 获取该物料类型的所有父ID
     * @param int $id
     * @return array
     */
    public static function getTypePids($id = 0)
    {
        $pids = [];
        //获取联动层数
        $level = self::getLevle($id);
        $map['status'] = 1;
        if(!empty($level)){
            $pids["level{$level}"] = $id;
            //根据联动层数遍历循环，并拼接父ID
            for ($i=$level;$i>0;$i--){
                if($i == $level)$pids["level{$i}"] = $id;
                else{
                    $map['id'] = $id;
                    $pids["level{$i}"] = $id = Db::name('wh_stock_type')->where($map)->value('pid');
                }
            }
        }

        return $pids;
    }

    // 设置物料类型选项卡
    public static function getTabNav($url = '',$pid = 0,$extend = []){

        $where = [['status','=',1],['pid','=',$pid]];

        //获取角色权限
        $ids = Db::name('admin_role')->where(['id'=>session('user_auth.role')])->value('stock_type_ids');
        if(!empty($ids) && empty($pid))$where[] = ['id','in',$ids];
        $list = Db::name('wh_stock_type')->where($where)->order('sort asc,id asc')->select();
        if(empty($list))return '';
        $list_tab = [];
        foreach ($list as $v){

            $pram['stock_type'] = $v['id'];

            if(!isset($extend['id']))$extend['id'] = '';
            $tab = [
                'id'            =>  "{$extend['id']}_stock_type_{$v['id']}",
                'title'         =>  $v['title'],
                'icon'          =>  $v['icon'],
                'url_value'     =>  url($url, $pram),
                'url_target'    =>  '',
                //'child'         =>  self::getTabNav($url,$v['id']),
                //'url'           =>  empty($pid)?'javascript:void(0);':url($url, $pram),
                //'child'         =>  self::getTabNav($url,$v['id']),
            ];
            $list_tab[$v['id']] = array_merge($extend,$tab);
        }

        if(empty($extend['id'])){
            $list_tab = [
                [
                    'id'            =>  "0",
                    'title'         =>  '物料分类',
                    'icon'          =>  '',
                    'url_value'     =>  '',
                    'url_target'    =>  '',
                    'child'         =>  $list_tab,
                ]
            ];
        }

        return $list_tab;

    }
    // 设置选项卡
    public static function getTabNav2($url = ''){

        $list_tab = [
            'joinList'   =>  [
                'id'            =>  "joinList",
                'title'         =>  '入库',
                'icon'          =>  '',
                'url_value'     =>  url('wh/stock/joinList'),
                'url_target'    =>  '',
            ],
            'outList'   =>  [
                'id'            =>  "outList",
                'title'         =>  '出库',
                'icon'          =>  '',
                'url_value'     =>  url('wh/stock/outList'),
                'url_target'    =>  '',
            ],
        ];

        return [
            'tab_list' => $list_tab,
            'curr_tab' => $url,
        ];

    }
    // 获取第一条子类型
    public static function getTypePidFirst($pid = 0){

        //if($pid == 0)$pid = Db::name('wh_stock_type')->where([['status','=',1],['pid','=',$pid]])->order('sort asc,id asc')->value('id');
        $info = Db::name('wh_stock_type')->where([['status','=',1],['pid','=',$pid]])->order('sort asc,id asc')->value('id');

        return $info;

    }
    /**
     * nav选项卡颜色css样式
     * @param int $pid 父ID
     * @param string $color 颜色
     * @return array
     */
    public static function cssNavColor($pid = 0,$color = '')
    {
        $css = "
        .nav2-color{$pid} {
            background-color: {$color};
            border-color: {$color};
            border-bottom: 1px solid {$color};
        }
        .nav2-pills > li.nav2-active{$pid} > a, .nav2-pills > li.nav2-active{$pid} > a:hover, .nav2-pills > li.nav2-active{$pid} > a:focus {
            color: {$color};
            border-bottom-color: {$color};
        }
        ";

        return $css;
    }

}
